from typing import Any
from datetime import timedelta

from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm.session import Session
from common.deps import check_jwt_token, get_db
from core.response import response_code
from core.schemas.user import UserOutResponse
from datetime import datetime
from core.security import create_access_token
from core.schemas.user import UserCreate
from setting.web import settings
from core.schemas.user import UserProfile,UserUpdate,ChangeUserPassword
from datetime import datetime
import crud

router = APIRouter()


@router.post("/modifyProfile", summary="修改用户信息")
async def modify_user_profile(user_update:UserUpdate, db: Session = Depends(get_db),payload=Depends(check_jwt_token)):
    uid = payload.get("uid")
    res = crud.user.update_user(db,uid, user_update)
    if res != 0:
        return response_code.resp_200(data=res)
    return response_code.resp_500(message="修改失败")

@router.post("/changePassword", summary="修改用户密码")
async def change_user_password(new_pass:ChangeUserPassword, db: Session = Depends(get_db),payload=Depends(check_jwt_token)):
    uid = payload.get("uid")
    res = crud.user.change_password(db,uid, new_pass.password)
    if res != 0:
        return response_code.resp_200(data=res)
    return response_code.resp_500(message="修改失败")

@router.get("/getCurrentUser", summary="获取当前用户信息",response_model=UserOutResponse)
async def get_current_user( db: Session = Depends(get_db),payload=Depends(check_jwt_token)):
    uid = payload.get("uid")
    res = crud.user.get_user_by_uid(db,uid)
    if res != None:
        return response_code.resp_200(data=res)
        #return res.to_dict()
    return response_code.resp_500(message="获取失败")

@router.get("/ranking/statistics", summary="统计")
async def ranking_statistics( db: Session = Depends(get_db),payload=Depends(check_jwt_token)):
    uid = payload.get("uid")
    res = crud.user.statistics(db)
    if res != None:
        return response_code.resp_200(data=res)
        #return res.to_dict()
    return response_code.resp_500(message="获取失败")

@router.get("/statistics", summary="统计")
async def self_statitics( db: Session = Depends(get_db),payload=Depends(check_jwt_token)):
    uid = payload.get("uid")
    res = crud.user.self_statitics(db,uid)
    if res != None:
        return response_code.resp_200(data=res)
        #return res.to_dict()
    return response_code.resp_500(message="获取失败")

@router.get("/participationStatistics", summary="统计")
async def participation_statitics( db: Session = Depends(get_db),payload=Depends(check_jwt_token)):
    uid = payload.get("uid")
    res = crud.user.participation_statitics(db,uid)
    if res != None:
        return response_code.resp_200(data=res)
        #return res.to_dict()
    return response_code.resp_500(message="获取失败")

@router.get("/list", summary="列出用户")
async def list_user(page: int = 1, pageSize: int = 20, db: Session = Depends(get_db)):
    res = crud.user.get_users(db,page,pageSize)
    return response_code.resp_200(data=res)
